package vcp.server.GUI;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;

import vcp.client.GUI.events.ConnectionRequestEvent;
import vcp.client.interfaces.listeners.IConnectionListener;
import vcp.enums.TimeChoose;

/**
 * 
 * @author VCP team server view GUI class
 */
public class ServerView extends JFrame {

	private static final long serialVersionUID = 1L;

	private final IConnectionListener connectionListener;
	private JLabel lblIPAddress;
	private JButton btnTryConnection;
	private JFormattedTextField ftfPot;
	private JComboBox<TimeChoose> cbTimeChoose;

	/**
	 * class constructor
	 * 
	 * @param connectionListener
	 */
	public ServerView(IConnectionListener connectionListener) {
		super("Server");
		this.connectionListener = connectionListener;

		this.setSize(new Dimension(373, 204));

		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		this.setupLayout();

		this.setVisible(true);
	}

	/**
	 * sets IP label
	 * 
	 * @param ip
	 */
	public void setIPLabel(String ip) {
		this.lblIPAddress.setText(ip);
	}

	public TimeChoose getSelectedTimeChoose() {
		return (TimeChoose) this.cbTimeChoose.getSelectedItem();
	}

	/**
	 * disables connection option
	 */
	public void disableConnectionOption() {
		this.btnTryConnection.setEnabled(false);
		this.ftfPot.setEditable(false);
		this.cbTimeChoose.setEnabled(false);
	}

	/** shows error message **/
	public void showError(String errorMessage) {
		JOptionPane.showMessageDialog(this, errorMessage, "Error",
				JOptionPane.ERROR_MESSAGE);
	}

	/** sets up the lay out **/
	private void setupLayout() {
		this.getContentPane().setLayout(null);

		JLabel lblNewLabel = new JLabel("Port:");
		lblNewLabel.setBounds(6, 16, 41, 34);
		this.getContentPane().add(lblNewLabel);
		lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel.setFont(new Font("Lucida Grande", Font.PLAIN, 18));

		this.ftfPot = new JFormattedTextField();
		this.ftfPot.setBounds(51, 16, 158, 34);
		this.getContentPane().add(this.ftfPot);
		this.ftfPot.setFont(new Font("Lucida Grande", Font.PLAIN, 18));

		this.lblIPAddress = new JLabel("");
		this.lblIPAddress.setBounds(219, 20, 135, 23);
		this.getContentPane().add(this.lblIPAddress);
		this.lblIPAddress.setVerticalAlignment(SwingConstants.BOTTOM);
		this.lblIPAddress.setFont(new Font("Tahoma", Font.PLAIN, 18));
		this.lblIPAddress.setForeground(Color.RED);

		this.btnTryConnection = new JButton("Try Connection");
		this.btnTryConnection
				.setFont(new Font("Lucida Grande", Font.PLAIN, 16));
		this.btnTryConnection.setBounds(208, 147, 159, 29);
		this.getContentPane().add(this.btnTryConnection);
		this.btnTryConnection.setPreferredSize(new Dimension(130, 29));
		this.btnTryConnection.setHorizontalTextPosition(SwingConstants.LEADING);
		this.btnTryConnection.setHorizontalAlignment(SwingConstants.RIGHT);
		this.btnTryConnection.setRequestFocusEnabled(false);

		JLabel lblNewLabel_1 = new JLabel("Each minute will be:");
		lblNewLabel_1.setFont(new Font("Lucida Grande", Font.PLAIN, 16));
		lblNewLabel_1.setBounds(6, 101, 165, 34);
		this.getContentPane().add(lblNewLabel_1);

		this.cbTimeChoose = new JComboBox<>();
		this.cbTimeChoose.setModel(new DefaultComboBoxModel<TimeChoose>(
				TimeChoose.values()));
		this.cbTimeChoose.setFont(new Font("Lucida Grande", Font.PLAIN, 17));
		this.cbTimeChoose.setBounds(169, 105, 113, 30);
		this.getContentPane().add(this.cbTimeChoose);
		this.btnTryConnection.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (ServerView.this.connectionListener != null) {
					try {
						Integer port = Integer.parseInt(ServerView.this.ftfPot
								.getText());
						ServerView.this.connectionListener
								.onConnectionRequest(new ConnectionRequestEvent(
										this, null, port));
					} catch (NumberFormatException exception) {
						ServerView.this.showError("Only numbers");
					}
				}
			}
		});
	}
}
